﻿Imports Microsoft.WindowsCE.Forms

Public Class frmReadOpt
    Dim mPrjOpt As Db_CurrentOption.CurrentOption

    Private Sub frmReadOpt_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mDB As New Db_CurrentOption
        mPrjOpt = mDB.Read()
        If mPrjOpt.CurrentProject = String.Empty Then
            MsgBox("请设置当前工程！")
            Me.Close()
        Else
            lblPrj.Text = mPrjOpt.CurrentProject
            lblPrj1.Text = mPrjOpt.CurrentProject

            txtWeather.Text = mPrjOpt.Weather
            txtWkSv.Text = mPrjOpt.Surveyor
            txtWkRec.Text = mPrjOpt.Recordor
            txtWkExam.Text = mPrjOpt.Examor
            txtInstr.Text = mPrjOpt.InstrNo
            txtStaff1.Text = mPrjOpt.Staff1
            txtStaff2.Text = mPrjOpt.Staff2


        End If
        DisplayExistFile()
        InputPanel1.Enabled = True
        txtCompareFile.Focus()

    End Sub

    Private Sub txtCompareFile_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCompareFile.GotFocus
        lstPrjFl.Items.Clear()
        lstPrjFl.Items.Add("比对文件：")
        DisplayExistFile()
    End Sub
    Private Sub DisplayExistFile()
        Dim pf As New PathAndFile(mPrjOpt.CurrentProject)
        Dim sfile() As String = PathAndFile.GetExistFiles.Trim.Split(",")
        If sfile.Length = 1 And sfile(0).Length < 2 Then
           
        Else
            Array.Sort(sfile)
            For Each mfile As String In sfile
                Dim Exist As Boolean = False
                If mfile.Length > 0 Then
                    For Each lst As String In lstPrjFl.Items
                        If lst = mfile Then
                            Exist = True
                            Exit For
                        End If
                    Next
                    If Not Exist Then lstPrjFl.Items.Add(mfile)
                End If
            Next
            If lstPrjFl.Items.Count > 3 Then lstPrjFl.TopIndex = lstPrjFl.Items.Count - 4
        End If
    End Sub

    Private Sub txtSaveTo_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles txtSaveTo.GotFocus
        lstPrjFl.Items.Clear()
        lstPrjFl.Items.Add("数据记录到：")
        DisplayExistFile()
        If lstPrjFl.Items.Count > 4 Then
            For I As Integer = 2 To lstPrjFl.Items.Count - 4
                lstPrjFl.Items.RemoveAt(1)
            Next
        End If
    End Sub

    ''' <summary>
    ''' 选择已有工程或已有文件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lstPrjFl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPrjFl.SelectedIndexChanged

        If  lstPrjFl.Items(0) = "比对文件：" Then
            If lstPrjFl.SelectedIndex > 0 Then
                txtCompareFile.Text = lstPrjFl.SelectedItem
                SetSaveFileText()
            End If
        ElseIf lstPrjFl.Items(0) = "本次数据记录到：" Then
            If lstPrjFl.SelectedIndex > 0 Then
                txtSaveTo.Text = lstPrjFl.SelectedItem
            End If
        End If

    End Sub


    ''' <summary>
    ''' 根据比对文件，设置保存文件的文件名
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetSaveFileText()
        Dim sTime As Integer = 0
        If txtCompareFile.Text.Length > 0 Then
            Dim sLastTurn As String = txtCompareFile.Text
            Dim pos As Integer = InStr(sLastTurn, "-")
            If pos > 0 Then
                sTime = CInt(Mid(sLastTurn, pos + 1)) + 1
            End If
        End If
        txtSaveTo.Text = Today.ToString("yyMMdd") & "-" & String.Format("{0:D3}", sTime)
        txtSaveTo.SelectionStart = txtSaveTo.Text.Length
        'txtSaveTo.Focus()
    End Sub

    Private Sub InputPanel1_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputPanel1.EnabledChanged
        If InputPanel1.Enabled Then
            TabControl1.Height = Me.Height - InputPanel1.Bounds.Height
        Else
            TabControl1.Height = Me.Height
        End If
    End Sub


    Private Sub txtWkSv_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles txtWkSv.GotFocus, txtWkRec.GotFocus, txtWkExam.GotFocus, txtWeather.GotFocus
        'If Not InputPanelSet.CurrenMod = "Ch" Then
        '    InputPanelSet.SetChinese(InputPanel1)
        'End If

        Dim txt As TextBox = CType(sender, TextBox)
        SetSelectList(txt)
    End Sub
    Private Sub txtInstr_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles txtInstr.GotFocus, txtStaff1.GotFocus, txtStaff2.GotFocus
        'If InputPanelSet.CurrenMod = "Ch" Then InputPanelSet.SetNumeric(InputPanel1)
        Dim txt As TextBox = CType(sender, TextBox)
        SetSelectList(txt)
    End Sub
    Private Sub SetSelectList(ByRef txt As TextBox)
        ListBox1.Top = txt.Bottom
        ListBox1.Left = txt.Left
        ListBox1.Width = txt.Width
        ListBox1.Items.Clear()

        Dim sTmp As String = String.Empty
        Select Case txt.Name
            Case "txtInstr"
                Dim dbInstr As New Db_Instrument
                sTmp = dbInstr.Read("水准仪")

            Case "txtStaff1", "txtStaff2"
                Dim dbInstr As New Db_Instrument
                sTmp = dbInstr.Read("水准尺")

            Case "txtWkExam", "txtWkRec", "txtWkSv"
                Dim dbWk As New Db_Workers
                sTmp = dbWk.Read()

            Case "txtWeather"
                sTmp = "晴" _
                        & vbCrLf & "多云" _
                        & vbCrLf & "阴" _
                        & vbCrLf & "小雨" _
                        & vbCrLf & "雪"


        End Select
        Dim vs() As String = sTmp.Split(vbCrLf)
        For Each sn As String In vs
            If sn.Trim.Length > 0 Then
                Dim sv() As String = sn.Split(",")
                ListBox1.Items.Add(sv(sv.Length - 1).Trim)
            End If
        Next

        If ListBox1.Items.Count > 0 Then
            ListBox1.Visible = True
        End If
    End Sub


    ''' <summary>
    ''' 更入文本框值
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim txt As TextBox
        With ListBox1
            If .Left = txtInstr.Left And .Top = txtInstr.Bottom Then
                txt = CType(txtInstr, TextBox)
            ElseIf .Left = txtStaff1.Left And .Top = txtStaff1.Bottom Then
                txt = CType(txtStaff1, TextBox)
            ElseIf .Left = txtStaff2.Left And .Top = txtStaff2.Bottom Then
                txt = CType(txtStaff2, TextBox)
            ElseIf .Left = txtWkExam.Left And .Top = txtWkExam.Bottom Then
                txt = CType(txtWkExam, TextBox)
            ElseIf .Left = txtWkRec.Left And .Top = txtWkRec.Bottom Then
                txt = CType(txtWkRec, TextBox)
            ElseIf .Left = txtWkSv.Left And .Top = txtWkSv.Bottom Then
                txt = CType(txtWkSv, TextBox)
            ElseIf .Left = txtWeather.Left And .Top = txtWeather.Bottom Then
                txt = CType(txtWeather, TextBox)
            Else
                txt = New TextBox
            End If
        End With
        txt.Text = ListBox1.SelectedItem
        ListBox1.Visible = False
        bReturn.Focus()
    End Sub


    Private Sub frmReadOpt_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        SaveSet()
    End Sub
    Private Sub bReturn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bReturn.Click
        ListBox1.Visible = False
        SaveSet()
        Me.Close()

    End Sub
    Private Sub SaveSet()
        With mPrjOpt
            .CompareFile = txtCompareFile.Text
            .Examor = txtWkExam.Text
            .InstrNo = txtInstr.Text
            .RecordFile = txtSaveTo.Text
            .Recordor = txtWkRec.Text
            .Staff1 = txtStaff1.Text
            .Staff2 = txtStaff2.Text
            .Surveyor = txtWkSv.Text
            .Weather = txtWeather.Text
        End With
        Dim mDb As New Db_CurrentOption
        mDb.Record(mPrjOpt)

        '更新作业组表
        Dim dbWorks As New Db_Workers
        If txtWkSv.Text.Length > 0 Then dbWorks.RecordRow(txtWkSv.Text.Trim)
        If txtWkRec.Text.Length > 0 Then dbWorks.RecordRow(txtWkRec.Text.Trim)
        If txtWkExam.Text.Length > 0 Then dbWorks.RecordRow(txtWkExam.Text.Trim)

        '更新仪器表
        Dim dbInstr As New Db_Instrument
        If txtInstr.Text.Length > 0 Then dbInstr.RecordRow("水准仪", txtInstr.Text.Trim)
        If txtStaff1.Text.Length > 0 Then dbInstr.RecordRow("水准尺", txtStaff1.Text.Trim)
        If txtStaff2.Text.Length > 0 Then dbInstr.RecordRow("水准尺", txtStaff2.Text.Trim)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TabControl1.SelectedIndex = 1
    End Sub
End Class

